<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="病人名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入病人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="就诊卡ID" prop="cardId">
        <el-input
          v-model="queryParams.cardId"
          placeholder="请输入就诊卡ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['his:patient:add']"
        >新办就诊卡</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['his:patient:edit']"
        >修改</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="patientList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="病人名称" align="center" prop="name" />
      <el-table-column label="身份证" align="center" prop="idCard" />
      <el-table-column label="性别" align="center" prop="sex">
        <!--<template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>-->
      </el-table-column>
      <el-table-column label="民族" align="center" prop="nationality" />
      <el-table-column label="出生日期" align="center" prop="birthday" />
<!--      <el-table-column label="UUID自动生成,随机" align="center" prop="id" />-->
      <el-table-column label="住址" align="center" prop="address" />
      <el-table-column label="就诊卡ID" align="center" prop="cardId" />
<!--      <el-table-column label="手机号(非必填)" align="center" prop="telphone" />-->
      <el-table-column label="创建时间" align="center" prop="createDatetime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['his:patient:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['his:patient:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改挂号收费对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%"  append-to-body>

      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="病人名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入病人名称" />
          </el-form-item>

        <el-form-item label="身份证" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证" />
        </el-form-item>

        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="民族" prop="nationality">
          <el-input v-model="form.nationality" placeholder="请输入民族" />
        </el-form-item>

        <el-form-item label="出生日期" prop="birthday">
          <el-input v-model="form.birthday" placeholder="请输入出生日期" />
        </el-form-item>

        <el-form-item label="住址" prop="address">
          <el-input v-model="form.address" placeholder="请输入住址" />
        </el-form-item>
        <el-form-item label="就诊卡ID" prop="cardId">
          <el-input v-model="form.cardId" placeholder="请输入就诊卡ID" />
        </el-form-item>
        <el-form-item label="手机号(非必填)" label-width="120px" prop="telphone">
          <el-input v-model="form.telphone" placeholder="请输入手机号(非必填)" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>








  </div>
</template>

<script>
  import { listPatient, getPatient, delPatient, addPatient, updatePatient, exportPatient } from "@/api/his/patient";
  import { listDepartment } from "@/api/his/department";
  export default {
    name: "Patient",
    dicts: ['sys_user_sex'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 挂号收费表格数据
        patientList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          idCard: null,
          sex: null,
          cardId: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            { required: true, message: "病人名称不能为空", trigger: "blur" }
          ],
          idCard: [
            { required: true, message: "身份证不能为空", trigger: "blur" }
          ],
          sex: [
            { required: true, message: "性别不能为空", trigger: "change" }
          ],
          nationality: [
            { required: true, message: "民族不能为空", trigger: "blur" }
          ],
          birthday: [
            { required: true, message: "出生日期不能为空", trigger: "blur" }
          ],
          address: [
            { required: true, message: "住址不能为空", trigger: "blur" }
          ],
          cardId: [
            { required: true, message: "就诊卡ID不能为空", trigger: "blur" }
          ],
        },
        ksOptions: [{
          "label": "呼吸内科",
          "value": 1
        }, {
          "label": "消化内科",
          "value": 2
        }, {
          "label": "泌尿内科",
          "value": 3
        }, {
          "label": "心内科",
          "value": 4
        }, {
          "label": "神经内科",
          "value": 5
        }, {
          "label": "小儿科",
          "value": 6
        }, {
          "label": "感染科",
          "value": 7
        }, {
          "label": "肝胆外科",
          "value": 8
        }, {
          "label": "泌尿外科",
          "value": 9
        }, {
          "label": "急诊科",
          "value": 10
        }, {
          "label": "体检",
          "value": 12
        }],
        //用于接收部门下拉框信息
        Departments:{},
        // 挂号弹窗开关
        openDep:false,
        formData: {
          name: undefined,
          type: undefined,
          field102: undefined,
        },

      };
    },
    created() {
      this.getList();
      this.getDepartment();
    },
    methods: {
      /** 查询挂号收费列表 */
      getList() {
        this.loading = true;
        listPatient(this.queryParams).then(response => {
          this.patientList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getDepartment() {
        this.loading = true;
        listDepartment(this.queryParams).then(response => {
          console.log(response);

          this.Departments=response.rows;
          console.log(Departments);

        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          name: null,
          idCard: null,
          sex: null,
          nationality: null,
          birthday: null,
          id: null,
          address: null,
          cardId: null,
          telphone: null,
          createDatetime: null,
          career: null,
          familyHistory: null,
          maritalStatus: "0",
          pastHistory: null,
          personalHistory: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加挂号收费";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getPatient(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改挂号收费";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updatePatient(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPatient(this.form).then(response => {
                debugger
                if(response.msg=="该病人信息已存在,不能重复注册"){
                  this.$modal.msgError(response.msg);
                    this.open = false;
                    this.getList();
                }else{
                  this.$modal.msgSuccess(response.msg);
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        console.log(row.id);
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除病人为"' + row.name + '"的数据项？').then(function() {
          return delPatient(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /**挂号按钮操作 */
      openGH(row) {
        this.openDep=true;


      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$modal.confirm('是否确认导出所有挂号收费数据项？').then(() => {
          this.exportLoading = true;
          return exportPatient(queryParams);
        }).then(response => {
          this.$download.name(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      },

    }
  };
</script>
